<!-- 商家申请管理 -->
<template>
  <div>
    <el-dialog
      title="驳回申请"
      :visible.sync="dialogformvisible"
      class="addUser"
      :close-on-click-modal="false"
      @close="clearFirmReason('denyFirm')"
    >
      <el-form
        :model="firmMessage"
        :rules="rules"
        ref="denyFirm"
        label-width="100px"
        class="demo-ruleForm denyFirm"
      >
        <el-form-item prop="reason">
          <el-input
            type="textarea"
            placeholder="请输入驳回原因"
            v-model="firmMessage.reason"
            minlength="20"
            maxlength="500"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearFirmReason('denyFirm')">取 消</el-button>
        <el-button
          type="primary"
          @click="denyFirmReason('denyFirm')"
          v-if="!loading"
          >确 定</el-button
        >
        <el-button type="primary" :loading="true" v-else>加载中</el-button>
      </div>
    </el-dialog>
    <!-- 信息表格 -->
    <tableData
      :visible.sync="dialogformvisible"
      :isdeny="isdeny"
      :ispass="ispass"
    ></tableData>
    <!-- 分页-->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page_number + 1"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="page.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="page.total_elements"
        v-if="page.total_elements != 0"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import tableData from "@/components/businessFirm/tableData.vue";
import { mapActions, mapState, mapGetters } from "vuex";
export default {
  components: {
    tableData,
  },
  data() {
    return {
      //驳回原因
      dialogformvisible: false,
      loading: false, //是否加载中...
      isdeny: true,
      ispass: true,
      rules: {
        reason: [
          {
            required: true,
            message: "请输入驳回原因，内容不少于20字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this["getData"]({ page: 0, size: 10 });
  },
  methods: {
    ...mapActions("adminBusiness", ["getData", "deny"]),
    //每页数量改变时触发
    handleSizeChange(val) {
      let count = this.$store.state.adminBusiness.page.total_elements;
      let page = this.$store.state.adminBusiness.page.page_number;
      page = page > Math.floor(count / val) ? Math.floor(count / val) : page;
      this["getData"]({ page, size: val });
    },
    //当前页数改变时触发
    handleCurrentChange(val) {
      this["getData"]({ page: val - 1, size: this["pageSize"] });
    },
    //驳回申请原因
    denyFirmReason(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let status = await this["deny"]({
            id: this["firmMessageId"],
            reason: this["firmMessageReason"],
          });
          if (status == 204) {
            this.loading = true;
            setTimeout(() => {
              this.loading = false;
              this.dialogformvisible = false;
              this.$message({
                type: "success",
                message: "驳回成功!",
              });
              setTimeout(() => {
                this["getData"]({
                  page: this["pageNumber"],
                  size: this["pageSize"],
                });
              }, 500);
            }, 3000);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //取消驳回
    clearFirmReason(refname) {
      this.$refs[refname].resetFields();
      this.dialogformvisible = false;
    },
  },
  computed: {
    ...mapState("adminBusiness", ["firmMessage", "page"]),
    ...mapGetters("adminBusiness", [
      "firmMessageId",
      "firmMessageReason",
      "pageNumber",
      "pageSize",
    ]),
  },
  watch: {
    // 监听mapState中的变量，当数据变化（有值、值改变等），
    // 保证能拿到完整的数据，不至于存在初始化没有数据的问题，然后可以赋给本组件data中的变量
  },
};
</script>
<style  lang='scss' scoped>
@import "@/assets/css/adminPerson_y.scss";
.denyFirm { 
  .el-form-item__content {
    margin-left: 0px !important;
    line-height: normal !important;
    textarea {
      min-height: 200px !important;
    }
  }
}
</style>
